package grails.company

import grails.converters.JSON
import grails.test.mixin.TestFor
import grails.test.mixin.TestMixin
import grails.test.mixin.domain.DomainClassUnitTestMixin
import grails.test.mixin.web.ControllerUnitTestMixin

/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(ExtHelperService)
@TestMixin([DomainClassUnitTestMixin, ControllerUnitTestMixin])
class ExtHelperServiceTests {

    // TODO: tests for deep versions

    void testOrganizationAsTreeNode() {
        mockDomain(Organization, [[title: 'ACME']])

        def org = Organization.findByTitle('ACME')
        def service = new ExtHelperService()

        def o = service.OrganizationAsTreeNode(org)

        assertNotNull o

        def parsed = JSON.parse(o)
        assertNotNull parsed
        assertEquals(parsed.text, 'ACME')
        assertEquals(parsed.domain, Organization.domain)
        assertEquals(parsed.subdomain, Organization.subdomain)
    }

    void testDepartmentAsTreeNode() {
        mockDomain(Organization, [[title:  'ACME']])
        mockDomain(Department, [[title: 'Cartoons']])
        def dep = Department.findByTitle('Cartoons')
        Organization.findByTitle('ACME').addToDepartments(dep)

        def service = new ExtHelperService()

        def d = service.DepartmentAsTreeNode(dep)

        assertNotNull d
        def parsed = JSON.parse(d)

        assertNotNull parsed
        assertEquals(parsed.text, 'ACME')
        assertEquals(parsed.domain, Department.domain)
        assertEquals(parsed.subdomain, Department.subdomain)
    }
}
